package com.antd.common.core.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.antd.admin.api.entity.SysDept;
import com.antd.admin.api.vo.SysDeptTreeVo;
import com.google.common.collect.Lists;
import lombok.experimental.UtilityClass;

import java.util.List;

/**
 * TODO
 *
 * @author 子丶沫
 * @version 1.0
 * @date 2020/11/24 11:45
 */
@UtilityClass
public class AntdDeptUtil {
    public void findDeptTreeChildren(List<SysDeptTreeVo> deptTreeVoList, List<SysDept> deptList) {
        for(SysDeptTreeVo sysDept:deptTreeVoList){
            List<SysDeptTreeVo> children= Lists.newArrayList();
            for(SysDept dept:deptList){
                SysDeptTreeVo deptTreeVo = new SysDeptTreeVo();
                BeanUtil.copyProperties(dept, deptTreeVo);
                if(ObjectUtil.isNotNull(sysDept.getParentId())&&sysDept.getDeptId().equals(dept.getParentId())){
                    deptTreeVo.setParentName(sysDept.getName());
                    deptTreeVo.setLevel(sysDept.getLevel()+1);
                    deptTreeVo.setTitle(dept.getName());
                    deptTreeVo.setKey(dept.getDeptId());
                    deptTreeVo.setLabel(dept.getName());
                    deptTreeVo.setValue(dept.getDeptId());
                    children.add(deptTreeVo);
                }
            }
            sysDept.setChildren(children);
            findDeptTreeChildren(children,deptList);
        }
    }
}
